package cn.buk.qms.web;

import cn.buk.common.Constant;
import cn.buk.tms.bill.service.BillImportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.beans.propertyeditors.CustomNumberEditor;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 *
 * @author yfdai 2017-01-11
 */
public class BaseController {

    @Autowired
    protected BillImportService importService;

    @InitBinder("sc")
    public void initBinder1(WebDataBinder binder) {
        binder.setFieldDefaultPrefix("sc.");
    }

    @InitBinder
    public void initBinder(WebDataBinder binder) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        dateFormat.setLenient(false);
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
    }

    @InitBinder
    public void initBinderCustomNumber(WebDataBinder binder) {
        binder.registerCustomEditor(Float.class, new CustomNumberEditor(Float.class, true));
    }

    @InitBinder("user")
    public void initBinderUser(WebDataBinder binder) {
        binder.setFieldDefaultPrefix("user.");
    }

    @InitBinder("customer")
    public void initBinder3(WebDataBinder binder) {
        binder.setFieldDefaultPrefix("customer.");
    }

    /**
     * 获取session中保存的enterprise_id
     * @param request
     * @return
     */
    protected int getEnterpriseId(HttpServletRequest request) {
        HttpSession session = request.getSession();
        Object obj = session.getAttribute(Constant.SESSION_ENTERPRISE_ID);
        return obj != null ? (int) obj : 0;
    }

    protected int getUserId(HttpServletRequest request) {
        HttpSession session = request.getSession();
        Object obj = session.getAttribute(Constant.SESSION_USERID);
        return obj != null ? (int) obj : 0;
    }

    protected String getUsername(HttpServletRequest request) {
        HttpSession session = request.getSession();
        Object obj = session.getAttribute(Constant.SESSION_USERNAME);
        return obj != null ? (String) obj : "";
    }

    protected String getUserRoles(HttpServletRequest request) {
        HttpSession session = request.getSession();
        Object obj = session.getAttribute("user_roles");
        return obj != null ? (String) obj : "";
    }



    protected void setSessionValue(HttpServletRequest request, String key, String value) {
        HttpSession session = request.getSession();
        session.setAttribute(key, value);
    }

    protected String getSessionValue(HttpServletRequest request, String key) {
        HttpSession session = request.getSession();
        Object obj = session.getAttribute(key);
        return obj != null ? (String) obj : "";
    }

    /**
     * 读取微信转发过来的xml数据
     * @param request
     * @return
     */
    protected String readInputXml(HttpServletRequest request) {
        String result = null;
        BufferedReader br;

        try {
            br = new BufferedReader(new InputStreamReader(request.getInputStream(), StandardCharsets.UTF_8));
            String line;
            StringBuilder sb = new StringBuilder();
            while ((line = br.readLine()) != null) {
                sb.append(line);
            }

            result = sb.toString();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return result;
    }

    protected PrintWriter generatePrintWriter(String filename, HttpServletResponse response) throws IOException {
        response.setHeader("Content-Disposition", "inline; filename=" + filename);
        response.setContentType("application/octet-stream");

        // 加上UTF-8文件的标识字符
        response.getOutputStream().write(
                new byte[]{(byte) 0xEF, (byte) 0xBB, (byte) 0xBF});
        return new PrintWriter(new OutputStreamWriter(response.getOutputStream(), StandardCharsets.UTF_8));
    }



}
